package test.hash;

import java.util.HashMap;
import java.util.HashSet;

public class Solution {
    public int subarraysWithKDistinct(int[] A, int K) {
        //在此处写入代码

        if (A.length < K) {
            return 0;
        }
        if (K <= 0) {
            return 0;
        }

        if (A.length == 1 && K == 1) {
            return 1;
        }
        int result = 0;

        for (int i = 0; i < A.length - 1; i++) {
            HashSet<Integer> set = new HashSet<Integer>();
            set.add(A[i]);
            if (K == 1) {
                result ++;
            }
            for (int j = i + 1; j < A.length; j++) {
                set.add(A[j]);
                if (set.size() > K) {
                    break;
                }
                if (set.size() == K) {
                    result ++;
                }
            }
        }

        return result;
    }
}
